package com.zxtyshangjia.zxtyjms.login.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.zxtyshangjia.zxtyjms.R;
import com.zxtyshangjia.zxtyjms.base.BaseBean;
import com.zxtyshangjia.zxtyjms.utils.Api;
import com.zxtyshangjia.zxtyjms.utils.SpUtils;
import com.zxtyshangjia.zxtyjms.utils.ToastUtil;
import com.zxtyshangjia.zxtyjms.utils.network.HttpUtil;
import com.zxtyshangjia.zxtyjms.utils.network.PostCallBack;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by 18222 on 2017/11/13.
 */

public class ModifyPasswordActivity extends Activity{

    /**
     * 账号
     */
    private String account;

    /**
     * 原密码
     */
    private EditText mOldPasswordET;
    private String mOldPassword;

    /**
     * 新密码
     */
    private EditText mNewPasswordET;
    private String mNewPassword;


    /**
     * 确认密码
     */
    private EditText mConPasswordET;
    private String mConpasswprd;

    /**
     * 修改 操作
     */
    private Button mCommit;

    //修改密码回调
    private PostCallBack modifyBack = new PostCallBack() {
        @Override
        public void onSuccess(Object data) {
            if(data != null && data instanceof BaseBean){
                BaseBean bean = (BaseBean) data;
                if(bean.flag.equals("success")){
                    ToastUtil.showToast(bean.message);
                    finish();
                }else {
                    ToastUtil.showToast(bean.message);
                }
            }
        }

        @Override
        public void onError(int code, String msg) {

        }
    };



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_possword);
        account = SpUtils.getInstance(this).getString("account","");
        bindView();

        mCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOldPassword = mOldPasswordET.getText().toString();
                mNewPassword = mNewPasswordET.getText().toString();
                mConpasswprd = mConPasswordET.getText().toString();
                if(mOldPassword.isEmpty()){
                    ToastUtil.showToast("原密码不能为空");
                }else if(mNewPassword.isEmpty()){
                    ToastUtil.showToast("请输入新密码");
                }else if(mConpasswprd.isEmpty()){
                    ToastUtil.showToast("请输入确认密码");
                }else if(!mNewPassword.equals(mConpasswprd)){
                    ToastUtil.showToast("您两次输入的密码不一致");
                }else {
                    //修改密码
                    Map<String,String> map = new HashMap<String, String>();
                    map.put("account",account);
                    map.put("old_password",mOldPassword);
                    map.put("new_password",mNewPassword);
                    map.put("r_password",mConpasswprd);
                    new HttpUtil(Api.MODIFY_PASSWORD_URL,map, BaseBean.class,modifyBack).postExecute();

                }
            }
        });
    }

    private void bindView() {

        mOldPasswordET = (EditText) findViewById(R.id.possword_et_old);
        mNewPasswordET = (EditText) findViewById(R.id.possword_et_new);
        mConPasswordET = (EditText) findViewById(R.id.possword_et_con);
        mCommit = (Button) findViewById(R.id.possword_bt_modify);

    }


}
